package cn.yitun.chat.yitunserverimcore.general.main.biz.chat.manager;

import cn.yitun.chat.yituncommonutil.YitunCollectionUtil;
import cn.yitun.chat.yitunservercommonlib.dataentity.data.core.chat.UserChatHistory;
import cn.yitun.chat.yitunservercommonlib.dataentity.data.message.chat.Content;
import cn.yitun.chat.yitunservercommonlib.dataentity.data.message.chat.Item;
import cn.yitun.chat.yitunservercommonlib.dataentity.data.message.chat.Section;
import cn.yitun.chat.yitunservercommonlib.dataentity.vo.system.user.UserVo;
import cn.yitun.chat.yitunservercommonlib.dataentity.vo.userchat.UserChatContentVo;
import cn.yitun.chat.yitunservercommonlib.dataentity.vo.userchat.UserChatItemVo;
import cn.yitun.chat.yitunserverimbase.dao.userchat.UserChatItemDao;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 
 * Date 2019-02-17 17:57:44<br>
 * Description
 * 
 * @author yitun<br>
 * @since 1.1
 */
@Service
public class UserChatHistoryManager {

	@Resource
	private UserChatItemDao userChatItemDAO;

	/**
	 * Date 2019-02-17 19:07:55<br>
	 * Description 拼装用户聊天内容列表
	 * @author yitun<br>
	 * @param chatContentList
	 * @param inverted 是否倒序排列
	 * @return
	 * @since 1.1
	 */
	public List<UserChatHistory> getUserChatHistoryList(List<UserChatContentVo> chatContentList, boolean inverted) {

		List<UserChatHistory> contents = new ArrayList<>();

		if (YitunCollectionUtil.isNotEmpty(chatContentList)) {
			List<Long> contentIds = new ArrayList<>();
			for (UserChatContentVo cc : chatContentList) {
				contentIds.add(cc.getId());
			}
			Map<Long, List<UserChatItemVo>> chatItemMap = new HashMap<>();
			if (YitunCollectionUtil.isNotEmpty(contentIds)) {
				List<UserChatItemVo> chatItemList = userChatItemDAO.getUserChatItemList(contentIds);

				for (UserChatItemVo item : chatItemList) {
					List<UserChatItemVo> list = chatItemMap.get(item.getUserChatContentId());
					if (null == list) {
						list = new ArrayList<>();
						chatItemMap.put(item.getUserChatContentId(), list);
					}
					list.add(item);
				}
			}

			for (UserChatContentVo cc : chatContentList) {

				UserChatHistory map = new UserChatHistory();
				Long messageKey = cc.getMessageId();
				Long contentId = cc.getId();
				Content content = new Content();
				UserVo receiveUser = new UserVo();
				UserVo sendUser = new UserVo();

				receiveUser.setHead(cc.getReceiveUserHead());
				receiveUser.setAvatar(cc.getReceiveUserAvatar());
				receiveUser.setId(cc.getReceiveUserId());
				receiveUser.setName(cc.getReceiveUserName());
				receiveUser.setNickname(cc.getReceiveUserNickname());

				sendUser.setHead(cc.getSendUserHead());
				sendUser.setAvatar(cc.getSendUserAvatar());
				sendUser.setId(cc.getSendUserId());
				sendUser.setName(cc.getSendUserName());
				sendUser.setNickname(cc.getSendUserNickname());

				List<UserChatItemVo> list = chatItemMap.get(cc.getId());
				List<Section> sections = new ArrayList<>();
				if (null != list && !list.isEmpty()) {
					int index = -1;
					Section section;
					List<Item> items = null;
					for (UserChatItemVo rci : list) {
						if (index != rci.getSection()) {
							index = rci.getSection();

							items = new ArrayList<>();
							section = new Section();
							section.setItems(items);

							sections.add(section);
						}

						Item item = new Item();
						item.setType(rci.getType());
						item.setValue(rci.getFilterValue());
						if (null != items) {
							items.add(item);
						}
					}
					content.setSections(sections);
					content.setKey(messageKey);
					content.setTimestamp(cc.getCreateTime());
					content.setId(cc.getId());

					map.setMessageId(messageKey);
					map.setContentId(contentId);
					map.setSendUser(sendUser);
					map.setReceiveUser(receiveUser);
					map.setContent(content);

					if (inverted) {
						contents.add(0, map);
					} else {
						contents.add(map);
					}
				}
			}
		}
		return contents;
	}
}
